package com.chw.admin.role;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.chw.constants.Constants;
import com.chw.utils.CommonFieldsUtil;

@Service
public class SysRoleService implements ISysRoleService {

	@Autowired
	private SysRoleMapper sysRoleMapper;
	
	@Autowired
	private SysRoleGroupMapper sysRoleGroupMapper;
	
	@Autowired
	private SysRoleUserMapper sysRoleUserMapper;
	
	@Override
	public PageInfo<SysRole> page(SysRole record, int currentPage, int pageSize) throws Exception {
		PageHelper.startPage(currentPage, pageSize);
		List<SysRole> list = sysRoleMapper.find(record);
		return new PageInfo<SysRole>(list);
	}

	@Override
	public SysRole selectById(String id) throws Exception {
		return sysRoleMapper.selectById(id);
	}

	@Override
	public void saveOrUpdate(SysRole record) throws Exception {
		if (record != null) {
			if (StringUtils.isNotBlank(record.getId())) {
				CommonFieldsUtil.setUpdateValue(record);
				sysRoleMapper.updateByIdSelective(record);
			}else{
				CommonFieldsUtil.setCreateValue(record);
				sysRoleMapper.insert(record);
			}
		}
	}

	@Override
	public void updateStatus(String id, String status) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		map.put("status", status);
		sysRoleMapper.updateSelective(map);
	}

	@Override
	public void delete(String id) throws Exception {
		SysRole record = new SysRole();
		record.setId(id);
		record.setEnabledFlag(Constants.ENABLED_FLAG_N);
		sysRoleMapper.updateByIdSelective(record);
	}

	@Override
	public void addGroupToSysRole(SysRoleGroup record) throws Exception {
		List<SysRoleGroup> list = sysRoleGroupMapper.find(record);
		if(list!=null && !list.isEmpty())
		{
			record = list.get(0);
			record.setEnabledFlag(Constants.ENABLED_FLAG_Y);
			CommonFieldsUtil.setUpdateValue(record);
			sysRoleGroupMapper.updateByIdSelective(record);
		}else{
			CommonFieldsUtil.setCreateValue(record);
			sysRoleGroupMapper.insert(record);
		}
	}

	@Override
	public void addUserToSysRole(SysRoleUser record) throws Exception {
		List<SysRoleUser> list = sysRoleUserMapper.find(record);
		if(list!=null && !list.isEmpty())
		{
			record = list.get(0);
			record.setEnabledFlag(Constants.ENABLED_FLAG_Y);
			CommonFieldsUtil.setUpdateValue(record);
			sysRoleUserMapper.updateByIdSelective(record);
		}else{
			CommonFieldsUtil.setCreateValue(record);
			sysRoleUserMapper.insert(record);
		}
	}

	@Override
	public List<Map<String, Object>> getUserByRole(SysRoleUser record) throws Exception {
		return sysRoleUserMapper.findMap(record);
	}

	@Override
	public List<Map<String, Object>> getGroupByRole(SysRoleGroup record) throws Exception {
		return sysRoleGroupMapper.findMap(record);
	}

	@Override
	public void deleteGroupFormRole(SysRoleGroup record) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("w_roleid", record.getRoleid());
		map.put("w_groupid", record.getGroupid());
		map.put("enabledFlag", Constants.ENABLED_FLAG_N);
		sysRoleGroupMapper.updateSelective(map);
	}

	@Override
	public void deleteUserFormRole(SysRoleUser record) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("w_roleid", record.getRoleid());
		map.put("w_userid", record.getUserid());
		map.put("enabledFlag", Constants.ENABLED_FLAG_N);
		sysRoleUserMapper.updateSelective(map);
	}

	@Override
	public boolean hasRoleName(String name,String id) throws Exception {
		SysRole role = new SysRole();
		role.setRolename(name);
		List<SysRole> list = sysRoleMapper.find(role);
		if(list!=null && list.size()==1){
			//当做修改操作检查名称是否存在时去除id本身
			if(StringUtils.isNotBlank(id))
			{
				role = list.get(0);
				if(id.equals(role.getId()))
				{
					return false;
				}
			}
			return true;
		}
		return false;
	}

}
